<?php
namespace app\astro\controller;
use \app\check\controller\Check;
use app\common\model\Recode;
use \think\Db;

class Transits extends Check
{
    public $recode = [];
    /**
     * @param null $datetime
     * @return string
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get($datetime = null)
    {
//        $datetime = htmlspecialchars(input('datetime'));
//        if($datetime === null) $datetime = htmlspecialchars(input('datetime'));
        $time = strtotime($datetime);
        $timeinfo = [
            'year'=>date('Y',$time),
            'month'=>date('m',$time),
            'day'=>date('d',$time),
            'hour'=>date('H',$time),
            'minute'=>date('i',$time),
        ];
        $timeinfo['timezone'] = (int)(input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'],$timeinfo['minute'],0,
            $timeinfo['month'],$timeinfo['day'],$timeinfo['year']);

        $map = ['id' => input('id/d', 0), 'uid' => $this->user['id']];

        if(empty($this->recode))
        {
            $RecodeModel = new Recode();

            $userinfo = $RecodeModel->getRecode($map);
        }else{
            $userinfo = $this->recode;
        }

        $Data = new Data();

        $timestamp1 = $Data->deal_time($userinfo);
        $utdatenow1 = strftime("%d.%m.%Y", $timestamp1);
        $utnow1 = strftime("%H:%M:%S", $timestamp1);
        $longitude1 = ($userinfo['long_deg'] + $userinfo['long_min'] / 60 ) * $userinfo['ew'];
        $latitude1 = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60 ) * $userinfo['ns'];

        $timestamp2 = $Data->deal_time($timeinfo);
        $utdatenow2 = strftime("%d.%m.%Y", $timestamp2);
        $utnow2 = strftime("%H:%M:%S", $timestamp2);
        $longitude2 = number_format(floatval(input('longitude')),8);
        $latitude2 = number_format(floatval(input('latitude')),8);

        $Set = new \app\user\controller\Astroset();

        $astro_set = $Set->get(true,'transits');// 内部调用
        $h_sys = $astro_set['value']['h_sys'];

        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $options1 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $h_sys];
        $options2 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $h_sys];

        $out1 = $Data->execData($options1);
        $out2 = $Data->execData($options2);

        $original_data1 = $Data->getOriginalData($out1,$h_sys);
        $original_data2 = $Data->getOriginalData($out2,$h_sys);
        $Data->array_remove($original_data1,$Data->planets_display);
        $Data->array_remove($original_data2,$Data->planets_display);

        $details1 = $Data->getPlanetHouseDetail($original_data1, $original_data1['hc1']);
        $phase1 = $Data->planetPkPlanet($original_data1, $original_data2, false,1);
        foreach ($phase1['planets'] as $k1 => $d1)
        {
            $details1['planets'][$k1]['phase'] = $d1['phase'];
            if(empty($d1['phase'])) $details1['planets'][$k1]['phase'] = null;
        }
        $details1['phase'] = $phase1['phase'];
        $details2 = $Data->getPlanetHouseDetail($original_data2, $original_data1['hc1']);
        $phase2 = $Data->planetPkPlanet($original_data2, $original_data1, false,2);
        foreach ($phase2['planets'] as $k2 => $d2)
        {
            $details2['planets'][$k2]['phase'] = $d2['phase'];
            if(empty($d2['phase'])) $details2['planets'][$k2]['phase'] = null;
        }
        $details2['phase'] = $phase2['phase'];

        $details1['planets'] = (object)$details1['planets'];// 指定输出为object  防止所有行星全开时 类型改变
        $details2['planets'] = (object)$details2['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        return json_encode(['code'=>200,'data'=>[$details1,$details2],'msg'=>'Success.']);
    }
}
